package com.zhb.cloud.distribute.lock;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: 分布式锁的接口
 * @Description: TODO描述本类的作用
 * @author: zhb zhb1316783262@163.com
 * @date: 2017年11月20日 下午4:10:55
 */
public interface DistributedLock {
	/**
	 * @Title: lock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:36
	 * @Description: 直接加锁
	 * @throws LockException
	 */
	void lock() throws LockException;

	/**
	 * @Title: lock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:41
	 * @Description: 锁定多长时间
	 * @param leaseTime
	 * @param unit
	 * @throws LockException
	 */
	void lock(long leaseTime, TimeUnit unit) throws LockException;

	/**
	 * @Title: unlock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:48
	 * @Description: 释放所
	 * @throws LockException
	 */
	void unlock() throws LockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:52
	 * @Description: Acquires the lock only if it is not held by another process at
	 *               the time of invocation.
	 * @return
	 * @throws LockException
	 */
	boolean tryLock() throws LockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:57
	 * @Description: Acquires the lock if it is not held by another thread within
	 *               the given waiting time and the current thread has not been
	 *               interrupted.
	 * @param waitTime
	 * @param unit
	 * @return
	 * @throws LockException
	 */
	boolean tryLock(long waitTime, TimeUnit unit) throws LockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:32:01
	 * @Description: Returns <code>true</code> as soon as the lock is acquired. If
	 *               the lock is currently held by another thread in this or any
	 *               other process in the distributed system this method keeps
	 *               trying to acquire the lock for up to <code>waitTime</code>
	 *               before giving up and returning <code>false</code>. If the lock
	 *               is acquired, it is held until <code>unlock</code> is invoked,
	 *               or until <code>leaseTime</code> have passed since the lock was
	 *               granted - whichever comes first.
	 * @param waitTime
	 * @param leaseTime
	 * @param unit
	 * @return
	 */
	boolean tryLock(long waitTime, long leaseTime, TimeUnit unit);

	/**
	 * @Title: isLocked
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:32:06
	 * @Description: 判断是否锁定
	 * @return
	 * @throws LockException
	 */
	boolean isLocked() throws LockException;
}
